#pragma once 
#include "vec3.h"
#include "GlobalDefine.h"
#include <vector>
#include <string>
#include "AABB.h"
#include "BaseAcc.h"
#include "AABB.h"
#include <array>
#include <fstream>
#include <assimp/Exporter.hpp>

namespace mray{

struct Ray;
struct Result;
struct Collible;
struct KDNode;
struct KDTODO;

struct KDTree : public BaseAcc{

	KDTree();
	~KDTree();

	void Build(const std::vector<Collible*>& collibles) override;
	bool Hit(Ray& ray, Result* pResult = nullptr)const override;

private:

	void RecursivelyBuild(const AABB& nodeBox, const std::vector<const AABB*>& boxes, KDNode* node, int depth);

	KDNode*				m_root;
	AABB				m_sceneBox;
	//parameters
	float				m_emptyBonus;
	float				m_edgeProtection;
	int					m_maxDepth;
	//temps
	std::vector<Edge>	m_tmpEdges[3];
	std::vector<AABB>	m_tmpBoxes;

	std::ofstream		m_ofsbin;
	std::ofstream		m_ofstxt;
	void log(const AABB& nodeBox);
	void dump()const;

};

}